package com.ny.demo509;
import java.io.*;

public class Test {
    private String name;
    private int age;
    private double salary;

    public Test(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public static void main(String[] args) {
        Test test1 = new Test("张三",18,3000);
        Test test2 = new Test("李四",30,5000);
        Test test3 = new Test("赵五",22,6700);
        String filePath = "example.txt";

        try(BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))){
            writer.write(test1.name+" "+test1.age+" "+test1.salary+" "+"\n");
            writer.write(test2.name+" "+test2.age+" "+test2.salary+" "+"\n");
            writer.append(test3.name+" "+test3.age+" "+test3.salary+" "+"\n");
            System.out.println(("文件写入成功"));
        } catch (IOException e) {
            System.err.println("写入文件时发生错误:" + e.getMessage());
            throw new RuntimeException(e);
        }

        System.out.println("\n开始读取文件内容:");
        try(BufferedReader reader = new BufferedReader(new FileReader(filePath))){
            String line;
            int lineNumber = 1;
            while((line = reader.readLine()) != null){
                System.out.println("第" + lineNumber + "行" + line);
                lineNumber++;
            }
        } catch (FileNotFoundException e) {
            System.err.println("读取文件时发生错误：" + e.getMessage());
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

